#include "delge_squirrelaudio.h"

void s_Audio_RegisterFunctions (HSQUIRRELVM v)
{
	SquirrelVM::CreateFunctionGlobal(s_Audio_LoadMusic,					_SC("LoadMusicA"),						_SC("*"));
	SquirrelVM::CreateFunctionGlobal(s_Audio_LoadSound,					_SC("LoadSoundA"),						_SC("*"));
	SquirrelVM::CreateFunctionGlobal(s_Audio_UnloadMusic,				_SC("UnloadMusicA"),					_SC("*"));
	SquirrelVM::CreateFunctionGlobal(s_Audio_UnloadSound,				_SC("UnloadSoundA"),					_SC("*"));
}

SQInteger s_Audio_LoadMusic (HSQUIRRELVM v)
{
	const SQChar			*sResourceName;
	const SQChar			*sFilepath;

	sq_getstring			(v, 2, &sResourceName);
	sq_getstring			(v, 3, &sFilepath);

	try
	{
		Music *pMusic = Globals::pGame->GetEngine()->GetAudio()->LoadMusic(string(sFilepath));
		ResourceManager<Music>::LoadResource(sResourceName, pMusic);
	}
	catch (DelGEError& error)
	{
		cout << "Error: " << error.description << endl;
	}

	return 0;
}

SQInteger s_Audio_LoadSound (HSQUIRRELVM v)
{
	const SQChar			*sResourceName;
	const SQChar			*sFilepath;

	sq_getstring			(v, 2, &sResourceName);
	sq_getstring			(v, 3, &sFilepath);

	try
	{
		Sound *pSound = Globals::pGame->GetEngine()->GetAudio()->LoadSound(string(sFilepath));
		ResourceManager<Sound>::LoadResource(sResourceName, pSound);
	}
	catch (DelGEError& error)
	{
		cout << "Error: " << error.description << endl;
	}

	return 0;
}

SQInteger s_Audio_UnloadMusic (HSQUIRRELVM v)
{
	const SQChar			*sResourceName;

	sq_getstring			(v, 2, &sResourceName);

	ResourceManager<Music>::UnloadResource(sResourceName);

	return 0;
}

SQInteger s_Audio_UnloadSound (HSQUIRRELVM v)
{
	const SQChar			*sResourceName;

	sq_getstring			(v, 2, &sResourceName);

	ResourceManager<Sound>::UnloadResource(sResourceName);

	return 0;
}